<template>
    <Modal v-model="isShow" :title="categoryTitle" :mask-closable="false" :closable="false">
        <Form :model="formItem" :label-width="80">
            <FormItem label="分类名称">
                <Input v-model="formItem.name" placeholder="请输入分类名称"></Input>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button @click="modelCancel" type="error" :loading="loadingBtn">取消</Button>
            <Button type="success" @click="handleSubmit(formItem)" :loading="loadingBtn">保存</Button>
        </div>
    </Modal>
</template>

<script>
import { CategoryInsert, CategoryUpdate } from '@/api/shop'
export default {
  name: 'AddEditCategory',
  props: {
    isShow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    categoryTitle: {
      type: String,
      default: () => {
        return '添加分类'
      }
    },
    type: {
      type: String,
      default: () => {
        return ''
      }
    },
    parentId: {
      type: String,
      default: () => {
        return ''
      }
    },
    parent: {
      type: String,
      default: () => {
        return ''
      }
    },
    formItemData: {
      type: Object,
      default: () => {
        return ''
      }
    }
  },

  data () {
    return {
      loadingBtn: false,
      formItem: {}
    }
  },
  methods: {
    handleSubmit (val) {
      switch (this.type) {
        case 'addDa':
          const param = {
            parent: '0',
            name: this.formItem.name
          }
          CategoryInsert(param).then(ret => {
            let data = ret.data
            if (data.errcode === 0) {
              this.loadingBtn = false
              this.success()
              this.formItem = {}
            } else {
              this.error(data)
            }
          })
          break
        case 'addChild':
          const param1 = {
            parent: this.parentId,
            name: this.formItem.name
          }
          CategoryInsert(param1).then(ret => {
            let data = ret.data
            if (data.errcode === 0) {
              this.loadingBtn = false
              this.success()
              this.formItem = {}
            } else {
              this.error(data)
            }
          })
          break
        case 'editDa':
          const param2 = {
            id: this.parentId,
            parent: this.parent,
            name: this.formItem.name
          }
          CategoryUpdate(param2).then(ret => {
            let data = ret.data
            if (data.errcode === 0) {
              this.loadingBtn = false
              this.success()
              this.formItem = {}
            } else {
              this.error(data)
            }
          })
      }
      this.$emit('submit')
    },
    modelCancel () {
      this.$emit('cancel')
    },
    success () {
      this.$Message.success('操作成功')
    },
    error (data) {
      this.$Message.error({
        content: '操作失败,\n' + data.errmsg
      })
    }
  },
  mounted () {
  },
  watch: {
    formItemData (val) {
      console.log(val, '监听数据')
      this.formItem = val
    }
  }
}
</script>

<style scoped>

</style>
